package com.xingda.packing.dmss.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xingda.packing.dmss.config.appset.AppSetParam;
import com.xingda.packing.dmss.domain.*;
import com.xingda.packing.dmss.enums.*;
import com.xingda.packing.dmss.http.WheelTorsionValueHttp;
import com.xingda.packing.dmss.minitor.dto.*;
import com.xingda.packing.dmss.minitor.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

@Slf4j
public class PackUtils {

    public static PkWheel toPkWheel(Wheel wheel) {
        PkWheel pkWheel = null;
        if (Objects.nonNull(wheel)) {
            pkWheel = new PkWheel();
            pkWheel.setBarCode(wheel.getBarCode());
            String vfree2 = wheel.getVFree2();
            pkWheel.setWheelType(WheelType.getByTypeName(vfree2).getTypeId());
            pkWheel.setQualifiedState(wheel.getQualityState().getCode());
            pkWheel.setIsFinished(wheel.getFinishedState().getCode());
            pkWheel.setTorqueValue(wheel.getTorsionValue());
            pkWheel.setSolder(wheel.getSolderJointCount());
            pkWheel.setMachineNo(wheel.getMachineNo());
            if (wheel.getAb() != null) {
                pkWheel.setSurfaceLr(WheelSurfaceType.getByAB(wheel.getAb()).getLr());
                pkWheel.setColor(wheel.getAb());
                pkWheel.setWheelTag(wheel.getWheelTag());
                pkWheel.setWheelTagMap(wheel.getWheelTagMap());
                pkWheel.setBarCodeBack(wheel.getBarCodeBack());
            }
            pkWheel.setWheelPk(wheel.getPkIntermediate());
            pkWheel.setIsValid(1);
            pkWheel.setConsumerName(wheel.getConsumerName());
            pkWheel.setScanTime(new Date());
            pkWheel.setEqType(wheel.getEqType());
            pkWheel.setPkInvbasdoc(wheel.getPkInvbasdoc());
            pkWheel.setDownTime(wheel.getDownTime());
            pkWheel.setFixedLength(wheel.getFixedLength());
        }
        return pkWheel;
    }

    public static PkShuntWheel toPkShuntWheel(Wheel wheel) {
        PkShuntWheel pkWheel = null;
        if (Objects.nonNull(wheel)) {
            pkWheel = new PkShuntWheel();
            pkWheel.setBarCode(wheel.getBarCode());
            String vfree2 = wheel.getVFree2();
            pkWheel.setWheelType(WheelType.getByTypeName(vfree2).getTypeId());
            pkWheel.setQualifiedState(wheel.getQualityState().getCode());
            pkWheel.setIsFinished(wheel.getFinishedState().getCode());
            pkWheel.setTorqueValue(wheel.getTorsionValue());
            pkWheel.setSolder(wheel.getSolderJointCount());
            pkWheel.setMachineNo(wheel.getMachineNo());
            pkWheel.setSurfaceLr(WheelSurfaceType.getByAB(wheel.getAb()).getLr());
            pkWheel.setWheelPk(wheel.getPkIntermediate());
            pkWheel.setIsValid(1);
            pkWheel.setConsumerName(wheel.getConsumerName());
            pkWheel.setScanTime(new Date());
            pkWheel.setEqType(wheel.getEqType());
            pkWheel.setPkInvbasdoc(wheel.getPkInvbasdoc());
            pkWheel.setDownTime(wheel.getDownTime());
            pkWheel.setFixedLength(wheel.getFixedLength());
            pkWheel.setWheelTag(wheel.getWheelTag());
            pkWheel.setWheelTagMap(wheel.getWheelTagMap());
            pkWheel.setColor(wheel.getAb());
            pkWheel.setBarCodeBack(wheel.getBarCodeBack());
        }
        return pkWheel;
    }

    public static Wheel toWheel(PkWheel pkWheel) {
        Wheel wheel = new Wheel();
        wheel.setAb(pkWheel.getColor());
        wheel.setBarCode(pkWheel.getBarCode());
        wheel.setFinishedState(WheelFinishedState.getByCode(pkWheel.getIsFinished()));
        wheel.setMachineNo(pkWheel.getMachineNo());
        wheel.setPkIntermediate(String.valueOf(pkWheel.getWheelPk()));
        wheel.setQualityState(WheelQualityState.getByCode(pkWheel.getQualifiedState()));
        wheel.setSolderJointCount(pkWheel.getSolder());
        wheel.setTorsionValue(pkWheel.getTorqueValue());
        wheel.setVFree2(Objects.requireNonNull(WheelType.getByTypeId(pkWheel.getWheelType())).getTypeName());
        wheel.setConsumerName(pkWheel.getConsumerName());
        wheel.setEqType(pkWheel.getEqType());
        wheel.setPkInvbasdoc(pkWheel.getPkInvbasdoc());
        wheel.setDownTime(pkWheel.getDownTime());
        wheel.setFixedLength(pkWheel.getFixedLength());
        wheel.setWheelTag(pkWheel.getWheelTag());
        wheel.setWheelTagMap(pkWheel.getWheelTagMap());
        return wheel;
    }

    // 装载工字轮信息时使用
    public static Wheel toWheel(PkWheelDto dto) {
        Wheel wheel = new Wheel();
        wheel.setAb(dto.getAb());
        wheel.setBarCode(dto.getBarCode());
        wheel.setFinishedState(WheelFinishedState.getByCode(dto.getIsFinished()));
        wheel.setMachineNo(dto.getMachineNo());
        wheel.setPkIntermediate(String.valueOf(dto.getWheelPk()));
        wheel.setQualityState(WheelQualityState.getByCode(dto.getQualifiedState()));
        wheel.setSolderJointCount(dto.getSolder());
        wheel.setTorsionValue(dto.getTorqueValue());
        wheel.setVFree2(Objects.requireNonNull(WheelType.getByTypeId(dto.getWheelType())).getTypeName());
        wheel.setConsumerName(dto.getConsumerName());
        wheel.setEqType(dto.getEqType());
        wheel.setPkInvbasdoc(dto.getPkInvbasdoc());
        wheel.setDownTime(dto.getDownTime());
        return wheel;
    }

    /**
     * 把信息中心拉取的中筒信息JSON数据转成 PkTaskBoxInfoDto对象
     */
    public static PkTaskBoxInfoDto toPkTaskBoxInfoDto(JSONObject packingInfoJson) {
        PkTaskBoxInfoDto pkTaskBoxInfoDto = null;
        if (Objects.nonNull(packingInfoJson) && !packingInfoJson.isEmpty()) {
            pkTaskBoxInfoDto = new PkTaskBoxInfoDto();
            pkTaskBoxInfoDto.setCustomerName(packingInfoJson.getString("vfree1"));
            pkTaskBoxInfoDto.setFixedLength(packingInfoJson.getInteger("fixedlength"));
            pkTaskBoxInfoDto.setBoxNo(packingInfoJson.getString("caseno"));
            pkTaskBoxInfoDto.setSurfaceLr(packingInfoJson.getString("vfree3"));
            String wheelTypeName = packingInfoJson.getString("vfree2");
            pkTaskBoxInfoDto.setWheelTypeName(wheelTypeName);
            pkTaskBoxInfoDto.setSpecifications(packingInfoJson.getJSONObject("invbasdoc").getString("invname"));
        }
        return pkTaskBoxInfoDto;
    }

    public static PackDemand toPackDemand(List<PkTaskDemand> pkTaskDemands) {
        PackDemand packDemand = null;
        if (Objects.nonNull(pkTaskDemands) && !pkTaskDemands.isEmpty()) {
            packDemand = new PackDemand();
            for (PkTaskDemand pkTaskDemand : pkTaskDemands) {
                PackDemandRange packDemandRange = new PackDemandRange(pkTaskDemand.getMinVal(), pkTaskDemand.getMaxVal(), pkTaskDemand.isEnable());
                if (DemandType.floorTorsion.getType().equals(pkTaskDemand.getType())) {
                    packDemand.setFloorTorsion(packDemandRange);
                } else if (DemandType.boxSolderNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.setBoxSolderNum(packDemandRange);
                } else if (DemandType.boxDeviation.getType().equals(pkTaskDemand.getType())) {
                    packDemand.setBoxDeviation(packDemandRange);
                } else if (DemandType.floorSameMachineNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.setFloorSameMachineNum(packDemandRange);
                } else if (DemandType.floor1SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderWheelNum().setFloor1SolderWheelNum(packDemandRange);
                } else if (DemandType.floor2SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderWheelNum().setFloor2SolderWheelNum(packDemandRange);
                } else if (DemandType.floor3SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderWheelNum().setFloor3SolderWheelNum(packDemandRange);
                } else if (DemandType.floor4SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderWheelNum().setFloor4SolderWheelNum(packDemandRange);
                } else if (DemandType.floor5SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderWheelNum().setFloor5SolderWheelNum(packDemandRange);
                } else if (DemandType.floor6SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderWheelNum().setFloor6SolderWheelNum(packDemandRange);
                } else if (DemandType.wheelTorsion.getType().equals(pkTaskDemand.getType())) {
                    packDemand.setWheelTorsion(packDemandRange);
                } else if (DemandType.floor1SolderNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderNum().setFloor1SolderNum(packDemandRange);
                } else if (DemandType.floor2SolderNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderNum().setFloor2SolderNum(packDemandRange);
                } else if (DemandType.floor3SolderNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderNum().setFloor3SolderNum(packDemandRange);
                } else if (DemandType.floor4SolderNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderNum().setFloor4SolderNum(packDemandRange);
                } else if (DemandType.floor5SolderNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderNum().setFloor5SolderNum(packDemandRange);
                } else if (DemandType.floor6SolderNum.getType().equals(pkTaskDemand.getType())) {
                    packDemand.getFloorSolderNum().setFloor6SolderNum(packDemandRange);
                }
            }
        }
        return packDemand;
    }

    public static List<PkTaskDemand> toPkTaskDemands(PkBoxInfoSetInput pkBoxInfoSetInput, int taskId) {
        List<PkTaskDemand> pkTaskDemands = null;
        if (Objects.nonNull(pkBoxInfoSetInput)) {
            pkTaskDemands = new ArrayList<>();
            PkTaskDemand floorTorsion = toPkTaskDemand(pkBoxInfoSetInput.getFloorTorsion(), DemandType.floorTorsion, taskId);
            if (Objects.nonNull(floorTorsion)) {
                pkTaskDemands.add(floorTorsion);
            }
            PkTaskDemand boxSolderNum = toPkTaskDemand(pkBoxInfoSetInput.getBoxSolderNum(), DemandType.boxSolderNum, taskId);
            if (Objects.nonNull(boxSolderNum)) {
                pkTaskDemands.add(boxSolderNum);
            }
            PkTaskDemand boxDeviation = toPkTaskDemand(pkBoxInfoSetInput.getBoxDeviation(), DemandType.boxDeviation, taskId);
            if (Objects.nonNull(boxDeviation)) {
                pkTaskDemands.add(boxDeviation);
            }
            PkTaskDemand floorSameMachineNum = toPkTaskDemand(pkBoxInfoSetInput.getFloorSameMachineNum(), DemandType.floorSameMachineNum, taskId);
            if (Objects.nonNull(floorSameMachineNum)) {
                pkTaskDemands.add(floorSameMachineNum);
            }
            PackFloorSolderWheelNumDemand packFloorSolderWheelNumDemand = pkBoxInfoSetInput.getFloorSolderWheelNum();
            if (Objects.nonNull(packFloorSolderWheelNumDemand)) {
                List<PkTaskDemand> packFloorSolderWheelNumDemandDemands = toPkTaskDemands(packFloorSolderWheelNumDemand, taskId);
                for (PkTaskDemand pkTaskDemand : packFloorSolderWheelNumDemandDemands) {
                    if (Objects.nonNull(pkTaskDemand)) {
                        pkTaskDemands.add(pkTaskDemand);
                    }
                }
            }
            PackFloorSolderNumDemand floorSolderNumDemand = pkBoxInfoSetInput.getFloorSolderNum();
            if (Objects.nonNull(floorSolderNumDemand)) {
                List<PkTaskDemand> packFloorSolderNumDemandDemands = toPkTaskDemand2s(floorSolderNumDemand, taskId);
                for (PkTaskDemand pkTaskDemand : packFloorSolderNumDemandDemands) {
                    if (Objects.nonNull(pkTaskDemand)) {
                        pkTaskDemands.add(pkTaskDemand);
                    }
                }
            }
            // 工字轮扭转
            PkTaskDemand wheelTorsionNum = toPkTaskDemand(pkBoxInfoSetInput.getWheelTorsion(), DemandType.wheelTorsion, taskId);
            if (Objects.nonNull(wheelTorsionNum)) {
                pkTaskDemands.add(wheelTorsionNum);
            }

        }
        return pkTaskDemands;
    }

    private static List<PkTaskDemand> toPkTaskDemand2s(PackFloorSolderNumDemand floorSolderNumDemand, int taskId) {
        List<PkTaskDemand> pkTaskDemands = null;
        if (Objects.nonNull(floorSolderNumDemand)) {
            pkTaskDemands = new ArrayList<>();
            pkTaskDemands.add(toPkTaskDemand(floorSolderNumDemand.getFloor1SolderNum(), DemandType.floor1SolderNum, taskId));
            pkTaskDemands.add(toPkTaskDemand(floorSolderNumDemand.getFloor2SolderNum(), DemandType.floor2SolderNum, taskId));
            pkTaskDemands.add(toPkTaskDemand(floorSolderNumDemand.getFloor3SolderNum(), DemandType.floor3SolderNum, taskId));
            pkTaskDemands.add(toPkTaskDemand(floorSolderNumDemand.getFloor4SolderNum(), DemandType.floor4SolderNum, taskId));
            pkTaskDemands.add(toPkTaskDemand(floorSolderNumDemand.getFloor5SolderNum(), DemandType.floor5SolderNum, taskId));
            pkTaskDemands.add(toPkTaskDemand(floorSolderNumDemand.getFloor6SolderNum(), DemandType.floor6SolderNum, taskId));
        }
        return pkTaskDemands;
    }

    public static List<PkTaskDemand> toPkTaskDemands(PackFloorSolderWheelNumDemand packFloorSolderWheelNumDemand, int taskId) {
        List<PkTaskDemand> pkTaskDemands = null;
        if (Objects.nonNull(packFloorSolderWheelNumDemand)) {
            pkTaskDemands = new ArrayList<>();
            pkTaskDemands.add(toPkTaskDemand(packFloorSolderWheelNumDemand.getFloor1SolderWheelNum(), DemandType.floor1SolderWheelNum, taskId));
            pkTaskDemands.add(toPkTaskDemand(packFloorSolderWheelNumDemand.getFloor2SolderWheelNum(), DemandType.floor2SolderWheelNum, taskId));
            pkTaskDemands.add(toPkTaskDemand(packFloorSolderWheelNumDemand.getFloor3SolderWheelNum(), DemandType.floor3SolderWheelNum, taskId));
            pkTaskDemands.add(toPkTaskDemand(packFloorSolderWheelNumDemand.getFloor4SolderWheelNum(), DemandType.floor4SolderWheelNum, taskId));
            pkTaskDemands.add(toPkTaskDemand(packFloorSolderWheelNumDemand.getFloor5SolderWheelNum(), DemandType.floor5SolderWheelNum, taskId));
            pkTaskDemands.add(toPkTaskDemand(packFloorSolderWheelNumDemand.getFloor6SolderWheelNum(), DemandType.floor6SolderWheelNum, taskId));
        }
        return pkTaskDemands;
    }

    public static PkTaskDemand toPkTaskDemand(PackDemandRange packDemandRange, DemandType demandType, int taskId) {
        PkTaskDemand pkTaskDemand = null;
        if (Objects.nonNull(packDemandRange)) {
            pkTaskDemand = new PkTaskDemand();
            pkTaskDemand.setMinVal(packDemandRange.getMin());
            pkTaskDemand.setMaxVal(packDemandRange.getMax());
            pkTaskDemand.setType(demandType.getType());
            pkTaskDemand.setEnable(packDemandRange.isEnable());
            pkTaskDemand.setTaskId(taskId);
            pkTaskDemand.setSortIndex(demandType.getSortIndex());
        }
        return pkTaskDemand;
    }


    /**
     * 把信息中心拉取的工字轮信息封装成工字轮对象
     *
     * @param resultJson 工字轮JSON信息
     */
    public static Wheel toWheel(JSONObject resultJson, int productTypeId) {
        Wheel wheel;
        if (Objects.isNull(resultJson)) {
            wheel = createEmptyWheel(productTypeId, new BigDecimal("999.0"));
        } else if (resultJson.isEmpty()) {
            wheel = createEmptyWheel(productTypeId, new BigDecimal("9999.0"));
        } else {
            wheel = new Wheel();
            JSONObject hwheel = resultJson.getJSONObject("hwheel");
            if (Objects.nonNull(hwheel) && !hwheel.isEmpty()) {
                wheel.setBarCode(hwheel.getString("code"));
            }
            // 工字轮类型 B80、B60
            wheel.setVFree2(resultJson.getString("vfree2"));
            // 客户名称
            wheel.setConsumerName(resultJson.getString("vfree1"));
            // 质量状态
            String pkQualitystate = resultJson.getString("pkQualitystate");
            WheelQualityState qualityState = pkQualitystate.equals(WheelQualityState.QUALITY.getPk()) ? WheelQualityState.QUALITY : pkQualitystate.equals(WheelQualityState.DISQUALITY.getPk()) ? WheelQualityState.DISQUALITY : WheelQualityState.WAIT_CHECK;
            wheel.setQualityState(qualityState);
            // 完成状态 半成品、成品
            String pkFinishedstate = resultJson.getString("pkFinishedstate");
            WheelFinishedState finishedState = pkFinishedstate.equals(WheelFinishedState.FINISHED.getPk()) ? WheelFinishedState.FINISHED : WheelFinishedState.SEMI_FINISHED;
            wheel.setFinishedState(finishedState);
            // 扭转值
            BigDecimal torsionValue = getTorsionValue(resultJson);
            if (Objects.isNull(torsionValue)) {
                JSONObject intermediatequalityJson = resultJson.getJSONObject("intermediatequality");
                torsionValue = getTorsionValue(intermediatequalityJson);
            }
            wheel.setTorsionValue(torsionValue == null ? new BigDecimal("9.9") : torsionValue);
            // 焊点数
            int solderjointcount = resultJson.getJSONArray("mmequipsolderjointlist") == null ? 0 : resultJson.getJSONArray("mmequipsolderjointlist").size();
            wheel.setSolderJointCount(solderjointcount);
            //定长
            wheel.setFixedLength(resultJson.getInteger("fixedlength"));
            wheel.setPkIntermediate(resultJson.getString("pkIntermediate"));
            // 卸轮信息
            JSONObject mmequipdownJson = resultJson.getJSONObject("mmequipdown");
            // 卸轮机床号
            String pkMmequip = null;
            // 卸轮类型
            String eqtype = null;
            if (Objects.nonNull(mmequipdownJson)) {
                pkMmequip = mmequipdownJson.getString("pkMmequip");
                JSONObject mmequipJson = mmequipdownJson.getJSONObject("mmequip");
                if (Objects.nonNull(mmequipJson)) {
                    eqtype = mmequipJson.getString("eqtype");
                }
                // 卸轮时间
                wheel.setDownTime(mmequipdownJson.getDate("downtime"));
            }
            if (pkMmequip == null) {
                String uuid = UUID.randomUUID().toString();
                log.warn("未找到机床pk值,随机复制为{}", uuid);
                pkMmequip = uuid;
            }
            wheel.setMachineNo(pkMmequip);
            wheel.setEqType(eqtype);
            // 物料信息编号
            String pkInvbasdoc = resultJson.getString("pkInvbasdoc");
            if (!StringUtils.hasText(pkInvbasdoc)) {
                JSONObject invbasdocJson = resultJson.getJSONObject("invbasdoc");
                if (!CollectionUtils.isEmpty(invbasdocJson)) {
                    pkInvbasdoc = invbasdocJson.getString("pkInvbasdoc");
                }
            }
            wheel.setPkInvbasdoc(pkInvbasdoc);
            // 工字轮状态
            int state = resultJson.getIntValue("state");
            wheel.setState(state);
            wheel.setWheelTag("#");
        }
        return wheel;
    }

    public static Wheel createEmptyWheel(int productTypeId, BigDecimal value) {
        Wheel wheel = new Wheel();
        wheel.setMachineNo(UUID.randomUUID().toString());
        wheel.setTorsionValue(value);
        wheel.setBarCode(UUID.randomUUID().toString());
        wheel.setVFree2(Objects.requireNonNull(WheelType.getByTypeId(productTypeId)).getTypeName());
        wheel.setQualityState(WheelQualityState.DISQUALITY);
        wheel.setFinishedState(WheelFinishedState.SEMI_FINISHED);
        wheel.setAb("Z");
        if (productTypeId == 1) {
            wheel.setWheelTag("NOT-AVAILABLE-B40-B60");
        } else {
            wheel.setWheelTag("NOT-AVAILABLE-B80");
        }
        log.info("创建不合格空轮！");
        return wheel;
    }

    /**
     * 把rabbitMQ得到的卸轮信息封装成工字轮对象 卸轮接收到消息和根据接口查出来的消息有部分区别，这里单独处理
     *
     * @param resultJson 工字轮JSON信息
     */
    public static Wheel toDownWheel(JSONObject resultJson) {
        Wheel wheel = null;
        if (Objects.nonNull(resultJson) && !resultJson.isEmpty()) {
            wheel = new Wheel();
            JSONObject hwheel = resultJson.getJSONObject("hwheel");
            if (Objects.nonNull(hwheel) && !hwheel.isEmpty()) {
                wheel.setBarCode(hwheel.getString("code"));
            }
            // 工字轮类型 B80、B60
            wheel.setVFree2(resultJson.getString("vfree2"));
            // 客户名称
            wheel.setConsumerName(resultJson.getString("vfree1"));
            // 质量状态
            String pkQualitystate = resultJson.getString("pkQualitystate");
            WheelQualityState qualityState = pkQualitystate.equals(WheelQualityState.QUALITY.getPk()) ? WheelQualityState.QUALITY : pkQualitystate.equals(WheelQualityState.DISQUALITY.getPk()) ? WheelQualityState.DISQUALITY : WheelQualityState.WAIT_CHECK;
            wheel.setQualityState(qualityState);
            // 完成状态 半成品、成品
            String pkFinishedstate = resultJson.getString("pkFinishedstate");
            WheelFinishedState finishedState = pkFinishedstate.equals(WheelFinishedState.FINISHED.getPk()) ? WheelFinishedState.FINISHED : WheelFinishedState.SEMI_FINISHED;
            wheel.setFinishedState(finishedState);
            // 扭转值
            BigDecimal torsionValue = getTorsionValue(resultJson);
            if (Objects.isNull(torsionValue)) {
                JSONObject intermediatequalityJson = resultJson.getJSONObject("intermediatequality");

                torsionValue = getTorsionValue(intermediatequalityJson);
            }
            wheel.setTorsionValue(torsionValue == null ? new BigDecimal("9999") : torsionValue);
            // 焊点数
            int solderjointcount = resultJson.getJSONArray("mmequipsolderjointlist") == null ? 0 : resultJson.getJSONArray("mmequipsolderjointlist").size();
            wheel.setSolderJointCount(solderjointcount);

            wheel.setPkIntermediate(resultJson.getString("pkIntermediate"));
            // 卸轮机床号
            String pkMmequip;
            // 卸轮类型
            String eqtype = null;
            pkMmequip = resultJson.getString("pkMmequip");
            JSONObject mmequipJson = resultJson.getJSONObject("mmequip");
            if (Objects.nonNull(mmequipJson)) {
                eqtype = mmequipJson.getString("eqtype");
            }
            if (pkMmequip == null) {
                String uuid = UUID.randomUUID().toString();
                log.warn("未找到机床pk值,随机复制为{}", uuid);
                pkMmequip = uuid;
            }
            wheel.setMachineNo(pkMmequip);
            wheel.setEqType(eqtype);
            // 物料信息编号
            String pkInvbasdoc = resultJson.getString("pkInvbasdoc");
            if (!StringUtils.hasText(pkInvbasdoc)) {
                JSONObject invbasdocJson = resultJson.getJSONObject("invbasdoc");
                if (!CollectionUtils.isEmpty(invbasdocJson)) {
                    pkInvbasdoc = invbasdocJson.getString("pkInvbasdoc");
                }
            }
            wheel.setPkInvbasdoc(pkInvbasdoc);
            // 卸轮时间
            wheel.setDownTime(resultJson.getDate("downtime"));
        }
        return wheel;
    }

    // 获得扭转值
    private static BigDecimal getTorsionValue(JSONObject json) {
        BigDecimal torsionValue = null;
        if (CollectionUtils.isEmpty(json)) {
            return null;
        }
        JSONArray intermediateinspectionlist = json.getJSONArray("intermediateinspectionlist");
        if (!CollectionUtils.isEmpty(intermediateinspectionlist)) {
            List<String> pkValues = new ArrayList<>();
            for (int i = 0; i < intermediateinspectionlist.size(); i++) {
                JSONObject intermediateinspection = intermediateinspectionlist.getJSONObject(i);
                torsionValue = intermediateinspection.getBigDecimal("nvalue");
                if (torsionValue != null) {
                    break;
                }
                String pkvalue = intermediateinspection.getString("pkvalue");
                if (Objects.nonNull(pkvalue)) {
                    pkValues.add(pkvalue);
                }
            }
            // 如果扭转值未获得
            if (Objects.isNull(torsionValue)) {
                torsionValue = WheelTorsionValueHttp.getTorsionValue(pkValues);
            }
        }
        return torsionValue;
    }


    public static PkBoxLayerDto toPkBoxLayerDto(JSONObject layerInfoJson) {
        PkBoxLayerDto pkBoxLayerDto = null;
        if (Objects.nonNull(layerInfoJson) && !layerInfoJson.isEmpty()) {
            pkBoxLayerDto = new PkBoxLayerDto();
            pkBoxLayerDto.setLayer(layerInfoJson.getInteger("layer"));
            pkBoxLayerDto.setTorqueValue(layerInfoJson.getDouble("torsionsum"));
            JSONArray items = layerInfoJson.getJSONArray("items");
            List<PkWheelLocationDto> wheelLocationList = toPkWheelLocationDtos(items);
            pkBoxLayerDto.setWheeLocationlList(wheelLocationList);
        }
        return pkBoxLayerDto;
    }

    private static List<PkWheelLocationDto> toPkWheelLocationDtos(JSONArray items) {
        int floorCount = items.size();
        List<PkWheelLocationDto> wheeLocationlList = new ArrayList<>();
        for (int j = 0; j < floorCount; j++) {
            wheeLocationlList.add(null);
        }
        List<Integer> sortList = AppSetParam.wheel_sort_index;
        for (int j = 0; j < floorCount; j++) {
            PkWheelLocationDto pkWheelLocationDto = new PkWheelLocationDto();
            JSONObject itemJson = items.getJSONObject(j).getJSONObject("intermediate");
            Wheel wheel = toWheel(itemJson, 0);
            PkWheel pkWheel = toPkWheel(wheel);
            pkWheelLocationDto.setPkWheel(pkWheel);
            pkWheelLocationDto.setLocationNo(j + 1);
            int sortIndex = sortList.get(j);
            wheeLocationlList.set(sortIndex, pkWheelLocationDto);
        }
        return wheeLocationlList;
    }

    public static PkBoxWheelDto toPkBoxWheelDto(JSONArray packinglists) {
        PkBoxWheelDto pkBoxWheelDto = null;
        if (Objects.nonNull(packinglists) && !packinglists.isEmpty()) {
            pkBoxWheelDto = new PkBoxWheelDto();
            int initLayer = packinglists.size();
            pkBoxWheelDto.setInitLayer(initLayer);
            pkBoxWheelDto.setInitWheelCount(initLayer * 12);
            List<PkBoxLayerDto> boxLayerInfos = new ArrayList<>();
            int size = packinglists.size();
            for (int i = 0; i < size; i++) {
                JSONObject layerInfoJson = packinglists.getJSONObject(i);
                PkBoxLayerDto pkBoxLayerDto = toPkBoxLayerDto(layerInfoJson);
                boxLayerInfos.add(pkBoxLayerDto);
            }
            pkBoxWheelDto.setBoxLayerInfos(boxLayerInfos);
        }
        return pkBoxWheelDto;
    }

    public static PkTaskBoxDemandDto toPkTaskBoxDemandDto(List<PkTaskDemand> pkTaskDemands) {
        PkTaskBoxDemandDto pkTaskBoxDemandDto = null;
        if (Objects.nonNull(pkTaskDemands) && !pkTaskDemands.isEmpty()) {
            pkTaskBoxDemandDto = new PkTaskBoxDemandDto();
            pkTaskBoxDemandDto.setFloorSolderWheelNum(new PackFloorSolderWheelNumDemand());
            pkTaskBoxDemandDto.setFloorSolderNum(new PackFloorSolderNumDemand());
            for (PkTaskDemand pkTaskDemand : pkTaskDemands) {
                if (DemandType.floorTorsion.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floorTorsion = new PackDemandRange();
                    floorTorsion.setMin(pkTaskDemand.getMinVal());
                    floorTorsion.setMax(pkTaskDemand.getMaxVal());
                    floorTorsion.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.setFloorTorsion(floorTorsion);
                } else if (DemandType.boxSolderNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange boxSolderNum = new PackDemandRange();
                    boxSolderNum.setMin(pkTaskDemand.getMinVal());
                    boxSolderNum.setMax(pkTaskDemand.getMaxVal());
                    boxSolderNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.setBoxSolderNum(boxSolderNum);
                } else if (DemandType.boxDeviation.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange boxDeviation = new PackDemandRange();
                    boxDeviation.setMin(pkTaskDemand.getMinVal());
                    boxDeviation.setMax(pkTaskDemand.getMaxVal());
                    boxDeviation.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.setBoxDeviation(boxDeviation);
                } else if (DemandType.floorSameMachineNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floorSameMachineNum = new PackDemandRange();
                    floorSameMachineNum.setMin(pkTaskDemand.getMinVal());
                    floorSameMachineNum.setMax(pkTaskDemand.getMaxVal());
                    floorSameMachineNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.setFloorSameMachineNum(floorSameMachineNum);
                } else if (DemandType.floor1SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor1SolderWheelNum = new PackDemandRange();
                    floor1SolderWheelNum.setMin(pkTaskDemand.getMinVal());
                    floor1SolderWheelNum.setMax(pkTaskDemand.getMaxVal());
                    floor1SolderWheelNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderWheelNum().setFloor1SolderWheelNum(floor1SolderWheelNum);
                } else if (DemandType.floor2SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor2SolderWheelNum = new PackDemandRange();
                    floor2SolderWheelNum.setMin(pkTaskDemand.getMinVal());
                    floor2SolderWheelNum.setMax(pkTaskDemand.getMaxVal());
                    floor2SolderWheelNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderWheelNum().setFloor2SolderWheelNum(floor2SolderWheelNum);
                } else if (DemandType.floor3SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor3SolderWheelNum = new PackDemandRange();
                    floor3SolderWheelNum.setMin(pkTaskDemand.getMinVal());
                    floor3SolderWheelNum.setMax(pkTaskDemand.getMaxVal());
                    floor3SolderWheelNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderWheelNum().setFloor3SolderWheelNum(floor3SolderWheelNum);
                } else if (DemandType.floor4SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor4SolderWheelNum = new PackDemandRange();
                    floor4SolderWheelNum.setMin(pkTaskDemand.getMinVal());
                    floor4SolderWheelNum.setMax(pkTaskDemand.getMaxVal());
                    floor4SolderWheelNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderWheelNum().setFloor4SolderWheelNum(floor4SolderWheelNum);
                } else if (DemandType.floor5SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor5SolderWheelNum = new PackDemandRange();
                    floor5SolderWheelNum.setMin(pkTaskDemand.getMinVal());
                    floor5SolderWheelNum.setMax(pkTaskDemand.getMaxVal());
                    floor5SolderWheelNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderWheelNum().setFloor5SolderWheelNum(floor5SolderWheelNum);
                } else if (DemandType.floor6SolderWheelNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor6SolderWheelNum = new PackDemandRange();
                    floor6SolderWheelNum.setMin(pkTaskDemand.getMinVal());
                    floor6SolderWheelNum.setMax(pkTaskDemand.getMaxVal());
                    floor6SolderWheelNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderWheelNum().setFloor6SolderWheelNum(floor6SolderWheelNum);
                } else if (DemandType.floor1SolderNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor1SolderNum = new PackDemandRange();
                    floor1SolderNum.setMin(pkTaskDemand.getMinVal());
                    floor1SolderNum.setMax(pkTaskDemand.getMaxVal());
                    floor1SolderNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderNum().setFloor1SolderNum(floor1SolderNum);
                } else if (DemandType.floor2SolderNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor2SolderNum = new PackDemandRange();
                    floor2SolderNum.setMin(pkTaskDemand.getMinVal());
                    floor2SolderNum.setMax(pkTaskDemand.getMaxVal());
                    floor2SolderNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderNum().setFloor2SolderNum(floor2SolderNum);
                } else if (DemandType.floor3SolderNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor3SolderNum = new PackDemandRange();
                    floor3SolderNum.setMin(pkTaskDemand.getMinVal());
                    floor3SolderNum.setMax(pkTaskDemand.getMaxVal());
                    floor3SolderNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderNum().setFloor3SolderNum(floor3SolderNum);
                } else if (DemandType.floor4SolderNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor4SolderNum = new PackDemandRange();
                    floor4SolderNum.setMin(pkTaskDemand.getMinVal());
                    floor4SolderNum.setMax(pkTaskDemand.getMaxVal());
                    floor4SolderNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderNum().setFloor4SolderNum(floor4SolderNum);
                } else if (DemandType.floor5SolderNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor5SolderNum = new PackDemandRange();
                    floor5SolderNum.setMin(pkTaskDemand.getMinVal());
                    floor5SolderNum.setMax(pkTaskDemand.getMaxVal());
                    floor5SolderNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderNum().setFloor5SolderNum(floor5SolderNum);
                } else if (DemandType.floor6SolderNum.getType().equals(pkTaskDemand.getType())) {
                    PackDemandRange floor6SolderNum = new PackDemandRange();
                    floor6SolderNum.setMin(pkTaskDemand.getMinVal());
                    floor6SolderNum.setMax(pkTaskDemand.getMaxVal());
                    floor6SolderNum.setEnable(pkTaskDemand.isEnable());
                    pkTaskBoxDemandDto.getFloorSolderNum().setFloor6SolderNum(floor6SolderNum);
                }
            }
        }
        return pkTaskBoxDemandDto;
    }


    /**
     * 把信息中心拉取的JSON数据转成PkTask对象
     */
    public static PkTask toPkTask(JSONObject packingInfoJson) {
        PkTask pkTask = null;
        if (Objects.nonNull(packingInfoJson) && !packingInfoJson.isEmpty()) {
            pkTask = new PkTask();
            pkTask.setPkPacking(packingInfoJson.getString("pkPacking"));
            pkTask.setPkInvBasDoc(packingInfoJson.getString("pkInvbasdoc"));
            pkTask.setPkDeptDoc(packingInfoJson.getString("pkDeptdoc"));
            pkTask.setPkCorp(packingInfoJson.getString("pkCorp"));
            pkTask.setPkCalbody(packingInfoJson.getString("pkCalbody"));
            String customerName = packingInfoJson.getString("vfree1");
            String wheelTypeName = packingInfoJson.getString("vfree2");
            String surfaceLr = packingInfoJson.getString("vfree3");
            String boxNo = packingInfoJson.getString("caseno");
            Integer fixedlength = packingInfoJson.getInteger("fixedlength");
            pkTask.setTaskName(customerName + "-" + wheelTypeName + "-" + surfaceLr + "-#" + boxNo);
            pkTask.setBoxNo(boxNo);
            pkTask.setCustomerName(customerName);
            pkTask.setFixedLength(fixedlength);
            JSONObject invbasdocJson = packingInfoJson.getJSONObject("invbasdoc");
            if (Objects.nonNull(invbasdocJson) && !invbasdocJson.isEmpty()) {
                pkTask.setSpecifications(invbasdocJson.getString("invname"));
                pkTask.setInvspec(invbasdocJson.getString("invspec"));
            }
            pkTask.setSurfaceLr(surfaceLr);
            Integer wheelTypeId = WheelType.getByTypeName(wheelTypeName).getTypeId();
            pkTask.setWheelTypeId(wheelTypeId);
            pkTask.setWheelTypeName(wheelTypeName);
            pkTask.setBoxWheelSum(packingInfoJson.getInteger("nassistnum"));
            pkTask.setLayer(packingInfoJson.getInteger("nnum"));
            JSONArray packinglists = packingInfoJson.getJSONArray("packinglists");
            int initLayer = 0;
            if (Objects.nonNull(packinglists) && !packinglists.isEmpty()) {
                initLayer = packinglists.size();
            }
            pkTask.setInitLayer(initLayer);
            pkTask.setInitWheelCount(initLayer * 12);
            pkTask.setFloorCount(packingInfoJson.getInteger("hsl"));
            pkTask.setBoxDescription(packingInfoJson.toJSONString());
            pkTask.setIsCurrentFinished(0);
        }
        return pkTask;
    }

    public static String getColorByWheelTypeNameAndSurfaceLr(String wheelTypeName, String surfaceLr) {
        if (wheelTypeName.equals("B80")) {
            if (surfaceLr.equals("左")) {
                return WheelSurfaceType.GREEN.getGr();
            }
            if (surfaceLr.equals("右")) {
                return WheelSurfaceType.RED.getGr();
            }
        }
        if (wheelTypeName.equals("B80/17")) {
            if (surfaceLr.equals("左")) {
                return WheelSurfaceType.BLUE.getGr();
            }
            if (surfaceLr.equals("右")) {
                return WheelSurfaceType.PINK.getGr();
            }
        }
        if (wheelTypeName.equals("B60")) {
            if (surfaceLr.equals("左")) {
                return WheelSurfaceType.GREEN.getGr();
            }
            if (surfaceLr.equals("右")) {
                return WheelSurfaceType.RED.getGr();
            }
        }
        if (wheelTypeName.equals("B40")) {
            if (surfaceLr.equals("左")) {
                return WheelSurfaceType.BLUE.getGr();
            }
            if (surfaceLr.equals("右")) {
                return WheelSurfaceType.PINK.getGr();
            }
        }
        return "异常";
    }

    /**
     * 转PkBom 允许装箱物料
     */
    public static List<PkBom> toPkBoms(JSONObject packingInfoJson) {
        List<PkBom> pkBoms = null;
        if (Objects.nonNull(packingInfoJson) && !packingInfoJson.isEmpty()) {
            JSONObject bomJson = packingInfoJson.getJSONObject("bom");
            if (!CollectionUtils.isEmpty(bomJson)) {
                JSONArray bomitems = bomJson.getJSONArray("bomitems");
                if (!CollectionUtils.isEmpty(bomitems)) {
                    pkBoms = new ArrayList<>();
                    for (int i = 0; i < bomitems.size(); i++) {
                        JSONObject bomitem = bomitems.getJSONObject(i);
                        String pkInvbasdoc = bomitem.getString("pkInvbasdoc");
                        JSONObject invbasdocJson = bomitem.getJSONObject("invbasdoc");
                        if (!CollectionUtils.isEmpty(invbasdocJson)) {
                            if (!StringUtils.hasText(pkInvbasdoc)) {
                                pkInvbasdoc = invbasdocJson.getString("pkInvbasdoc");
                            }
                            String invcode = invbasdocJson.getString("invcode");
                            String invname = invbasdocJson.getString("invname");
                            String invspec = invbasdocJson.getString("invspec");
                            if (StringUtils.hasText(pkInvbasdoc)) {
                                PkBom pkBom = new PkBom();
                                pkBom.setInvcode(invcode);
                                pkBom.setInvname(invname);
                                pkBom.setInvspec(invspec);
                                pkBom.setPkInvbasdoc(pkInvbasdoc);
                                pkBoms.add(pkBom);
                            }
                        }
                    }
                }
            }
        }
        return pkBoms;
    }
}
